<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>

<body>
    <button id="btn">按钮</button>
    <script>
        //单体模式  可以实例化，只能一次。所以点击按钮只会创建一次div
        //匿名函数自我执行
        var creatDiv = (function () {
            var odiv;
            return function () {
                if (!odiv) {
                    odiv = document.createElement("div");
                    odiv.innerHTML = "单体模式";
                    odiv.style.display = "none";
                    document.body.append(odiv);
                }
                return odiv;
            }
        })();
        var btn = document.getElementById("btn");
        btn.onclick = function () {
            var o = creatDiv();
            o.style.display = "block";
        }
    </script>
</body>

</html>